package com.wangp.mywrite.inwork.pattern._6._6_2_back_refer_replace;

import com.google.common.collect.ImmutableList;
import com.wangp.mywrite.inwork.pattern.InitData;
import java.util.ArrayList;
import java.util.List;

/***
 * @author wangpeng
 * @since 2022-03-03  16:04
 */
public class BackReferReplace {

    private static final ImmutableList<String> PHONE_LIST = ImmutableList.<String>builder()
            .add("313-555-1234")
            .add("248-555-9999")
            .add("810-555-9000")
            .build();

    public static void main(String[] args) {
        // 将邮件内容替换成 href 链接
        String context = "Hello, ben@forta.com is his email address.";
        // 括号必不可少
        String emailReg = "(\\w+[\\w.]*@[\\w]+\\.\\w+)";
        String replaceStr = "<A HREF = \"mailto:$1\">$1</A>";
        // Hello, <A HREF = "mailto:ben@forta.com">ben@forta.com</A> is his email address.
        System.out.println(context.replaceAll(emailReg, replaceStr));

        // 将手机号格式 313-555-1234 替换为 (313) 555-1234
        String matchReg = "(\\d{3})(-)(\\d{3})(-)(\\d{4})";
        String replaceReg = "($1) $3-$5";
        // [(313) 555-1234, (248) 555-9999, (810) 555-9000]
        System.out.println(InitData.replaceByReg(PHONE_LIST, matchReg, replaceReg));

        lookNote();

        // 把一级标题<H1></H1>的内容转换成大写
        String html = "<h1>Welcome to my home page </h1>";
        String matchH1 = "(<[Hh]1>)(.*?)(</[Hh]1>)";
        String convertBig = "$1\\U$2\\E$3";
        // Java 未生效，期望结果：<h1>WELCOME TO MY HOME PAGE </h1>
        System.out.println(InitData.replaceByReg(html,matchH1,convertBig));
    }

    /**
     * <table>
     *     <tr>
     *         <th>元字符</th>
     *         <th>说明</th>
     *     </tr>
     *     <tr>
     *         <th>\E</th>
     *         <th>结束 \L或\U</th>
     *     </tr>
     *     <tr>
     *         <th>\l</th>
     *         <th>把下一个字符转换成小写</th>
     *     </tr>
     *     <tr>
     *         <th>\L</th>
     *         <th>把 \L到\E之间的字符全部转换成小写</th>
     *     </tr>
     *     <tr>
     *         <th>\ u</th>
     *         <th>把下一个字符转换成大写</th>
     *     </tr>
     *     <tr>
     *         <th>\ U</th>
     *         <th>把 \L到\E之间的字符全部转换成大写</th>
     *     </tr>
     * </table>
     */
    private static void lookNote() {
    }

}
